﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;

namespace YouHP.Pages.Profile
{
    public class ActionAdd : YouHP.Pages.Profile.ProfileBasePage
    {
        public override void InitPage()
        {
            base.InitPage();
            this.PageKey = PageKeys.Profile_ActionAdd;
        }

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
        }

        protected LogTopicInfo LogTopicData;

        protected void HandleCommon()
        {
            this.AddLocation("添加表情交互", "");
            this.SetTitle("添加表情交互");
        }

        protected void HandlePost()
        {
            if (UserData.Id <= 0)
            {
                this.AddErrLine("请您登录");
                this.SetUrl("/User/Login.aspx");
                this.SetMetaRefresh();
                return;
            }
            if (ProfileData.Id == UserData.Id)
            {
                this.AddErrLine("您不能给自己发送表情");
                this.SetUrl("/Profile/ActionList.aspx?uid=" + UserData.Id);
                this.SetMetaRefresh();
                return;
            }

            ActionInfo info = ValidateForm();
            if (ErrorCount > 0) return;
            if (info == null) return;
            bool succ = ActionBLL.Instance.AddAction(info);

            string url = string.Format("/Profile/ActionList.aspx?uid={0}", ProfileData.Id);

            if (succ)
            {
                this.AddMsgLine("操作成功");
            }
            else
            {
                this.AddErrLine("操作失败");
            }
            this.SetUrl(url);
            this.SetMetaRefresh();
        }

        ActionInfo ValidateForm()
        {
            ActionInfo info = new ActionInfo();

            info.ActionType = RequestTools.GetQueryInt("actiontype",0);
            if (info.ActionType >= Constants.HugTypeCount)
            {
                this.AddErrLine("无效的表情");
                return null;
            }
            info.Remark = RequestTools.GetFormString("msg").Trim();
            if (info.Remark == "随便说两句") info.Remark = "没有附加陈述";
            if(info.Remark.Length > 1000) 
            {
                 this.AddErrLine("信息长度不能大于 1000 ");
                 return null;
            }
            info.ToUserId = ProfileData.Id;
            info.ToUserName = ProfileData.UserName;
            info.FromUserId = UserData.Id;
            info.FromUserName = UserData.UserName;
            info.PostTime = DateTime.Now;
            
            return info;
        }
    }
}
